$(document).ready(function() {
	$('#comment-entry-box').keydown(function (e){
		if(e.keyCode == 13){
			var comment = $('#comment-entry-box').val();
			if($('#comment-entry-box').val() != '') {
				$('#comment-entry-box').val(null);
				var selected_place_id = $('#final-selection-id').text();
				$.ajax({
					type:'POST',
					url:'submit_comment.php',
					data:'comment='+comment+'&place_id='+selected_place_id,
					dataType: 'json',
					error: function(request, status, error) {
						 alert(request.responseText);
					},
					success: function(response) {
						$("<div class='comment'>" +
								"<div id='comment-photo'>" +
									"<img src='images/" + response["user_photo"] + "'>" +
								"</div>" +
								"<div id='vote'>"+
									"<span class='up'>&#x25B2</span><br>" +
									"<span class='vote_count'>0</span><br>" +
									"<span class='down'>&#x25BC</span>" +
								"</div>" +
								"<div class='comment-body'>" +
									"<span id='comment-information'>" +
										"<span id='commenter-name'><a href='profile.php?id=" + response["user_id"] + "'>" + response["username"] + "</a></span>" +
										"<span id='comment-date-time' class='tiny-nl-info'>" + response["date_time"] + "</span>" +
									"</span>" +
									"<span id='delete' class='delete'>&#10006</span>" +
									"<p>" + response["body"] + "</p>" +
									"<span class='comment_id'>" + response["comment_id"] + "</span>" +
								"</div>" +
								"<div id='clear'></div>" +
						"</div>").prependTo('#comment-box').hide().slideDown();
						$('#empty-comments').fadeOut();
						$('#comment-box').find('.comment-body').on('mouseenter', function() {
							$(this).find("#delete").fadeIn('fast');
							$(this).find(".vote").fadeIn('fast');
						}).mouseleave(function() {
							$(this).find('#delete').fadeOut('fast');
							$(this).find('.vote').fadeOut('fast');
						});
						get_time();
					},
				});
			} else {
				error_report("Error Commenting", "The comment field is empty. Please type a comment before commenting.");
			}
		}
	})
});

$(document).on('click', '.delete',function() {
	var comment_id = $(this).parent().find('.comment_id').text();
	var box = this;
	$.ajax({
		type:'POST',
		url:'delete_comment.php',
		data:'comment_id='+comment_id,
		error: function(request, status, error) {
			 alert(request.responseText);
		},
		success: function(response) {
		},
		complete: function() {
			$(box).parent().parent().slideUp();
		},
	});
});

$(document).on('click', '.up',function() {
	var vote = parseInt($(this).parent().find('.vote_count').text());
	var comment_id = $(this).parent().parent().find('.comment-body').find('.comment_id').text();
	var this_vote = this;
	var v = 1;
	$.ajax({
		type:'POST',
		url:'submit_vote.php',
		data:'vote='+v+'&comment_id='+comment_id,
		error: function(request, status, error) {
			 alert(request.responseText);
		},
		success: function(response) {
			if(response == 1) {
				vote = vote + 1;
				$(this_vote).parent().parent().find('.vote_count').html(vote);
			} else if(response == 3) {
				//work out an algorithm for this whole - and + thing
				vote = vote + 2;
				$(this_vote).parent().parent().find('.vote_count').html(vote);
			} else {
				error_report("Error Voting", "You have already upvoted this comment.");
			}
		},
		complete: function() {
		},
	});	
});

$(document).on('click', '.down',function() {
	var vote = parseInt($(this).parent().find('.vote_count').text());
	var comment_id = $(this).parent().parent().find('.comment-body').find('.comment_id').text();
	var this_vote = this;
	var v = -1;
	$.ajax({
		type:'POST',
		url:'submit_vote.php',
		data:'vote='+v+'&comment_id='+comment_id,
		error: function(request, status, error) {
			 alert(request.responseText);
		},
		success: function(response) {
			if(response == 1) {
				vote = vote - 1;
				$(this_vote).parent().parent().find('.vote_count').html(vote);
			} else if(response == 3) {
				vote = vote -2;
				$(this_vote).parent().parent().find('.vote_count').html(vote);
			} else {
				error_report("Error Voting", "You have already downvoted this comment.");
			}
		},
		complete: function() {
		},
	});	
});